package com.geovis.emergency.spd.biz.pc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.pc.mapper.PcDrillEarlyDisposeMapper;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillEarlyDisposeService;
import com.geovis.emergency.spd.biz.pc.service.IPcSchedulingEventService;
import com.geovis.emergency.spd.biz.system.service.ISystemFileBusinessService;
import com.geovis.emergency.spd.biz.system.service.ISystemFileService;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillEarlyDispose;

import com.geovis.emergency.spd.entity.pc.entity.PcDrillPublishInfo;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDrillEarlyDisposeVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcSchedulingEventVO;
import com.geovis.emergency.spd.entity.system.constant.SystemFileBusinessConstant;
import com.geovis.emergency.spd.entity.system.entity.SystemFileBusiness;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 应急预案指挥模块-前期处置 服务实现类
 * </p>
 *
 * @author Lonsho
 * @since 2022-11-07
*/

@Service
@RequiredArgsConstructor
@Slf4j
public class PcDrillEarlyDisposeServiceImpl extends ServiceImpl<PcDrillEarlyDisposeMapper, PcDrillEarlyDispose> implements IPcDrillEarlyDisposeService {

    private final ISystemFileBusinessService systemFileBusinessService;
    private final IPcSchedulingEventService pcSchedulingEventService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEarlyDisposeBatch(List<PcDrillEarlyDisposeVO> list) {
        PcSchedulingEventVO detailEvent = null;
        for (PcDrillEarlyDisposeVO pcDrillEarlyDisposeVO : list) {
            if (ObjectUtil.isNull(detailEvent)){
                detailEvent = pcSchedulingEventService.getDetailEventByDrillId(list.get(0).getDrillId());
            }
            if (ObjectUtil.isNotNull(detailEvent)){
                pcDrillEarlyDisposeVO.setEventId(detailEvent.getId());
            }
            PcDrillEarlyDispose earlyDispose = BeanUtil.toBean(pcDrillEarlyDisposeVO, PcDrillEarlyDispose.class);
            saveOrUpdate(earlyDispose);
            // 处理附件
            if (CollUtil.isNotEmpty(pcDrillEarlyDisposeVO.getFileList())) {
                Set<SystemFileBusiness> fileBusinessSet = CollUtil.set(false);
                pcDrillEarlyDisposeVO.getFileList().forEach(file ->{
                    if (systemFileBusinessService.count(
                            Wrappers.lambdaQuery(SystemFileBusiness.class)
                                    .eq(SystemFileBusiness::getRelatedId,earlyDispose.getId())
                                    .eq(SystemFileBusiness::getFileId,file.getId())
                    ) == 0){
                        fileBusinessSet.add(new SystemFileBusiness(file.getId(), SystemFileBusinessConstant.TYPE_DRILL_EARLY_DISPOSE, earlyDispose.getId()));
                    }
                });
                if (CollUtil.isNotEmpty(fileBusinessSet)){
                    systemFileBusinessService.saveBatch(fileBusinessSet);
                }
            }
        }
    }
}
